import {onMounted, ref} from "vue";
import {useRouter} from "vue-router";
import service from "../service/service";

export default function Header() {
    let isLogin = ref(false)
    const router = useRouter()
    let SortList = ref([])
    onMounted(() => {
        let token = localStorage.getItem('token')
        if (token) {
            isLogin.value = true
        } else {
            isLogin.value = false
        }
        // 请求获取全部分类
        service.getSorts().then(r => {
            if (r.data.code === 200) {
                for (let i = 0; i < r.data['msg'].length; i++) {
                    let tmpData = r.data['msg']
                    let tmpObj = {}
                    tmpObj.name = tmpData[i]['Name']
                    tmpObj.url_name = tmpData[i]['UrlName']
                    SortList.value.push(tmpObj)
                }
            }
        })

    })
    const toHomeBtn = () => {
        window.location.href = '/#/'
    }
    const toLoginBtn = () => {
        if (isLogin.value) {
            router.push('/admin')
        } else {
            router.push('/login')
        }
    }
    const toClassifyBtn = (url_name) => {
        router.push({
            path: '/classify',
            query: {
                classify_name: url_name
            }
        })
        console.log(url_name)
    }

    let searchCon = ref('')
    const searchBtn = ()=>{
        router.push({
            path:'/search',
            query:{
                'content':searchCon.value
            }
        })
        console.log(searchCon.value)
    }
    return {
        toHomeBtn,
        toLoginBtn,
        isLogin,
        SortList,
        toClassifyBtn,
        searchCon,
        searchBtn
    }
}